package com.lsm;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelCreator {

    public static void toubu(String[] columns,Workbook workbook,Sheet sheet) {


        // 创建表头行
        Row headerRow = sheet.createRow(0);
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        for (int i = 0; i < columns.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(columns[i]);
            cell.setCellStyle(headerStyle);
        }
    }


    public static void weibu(String filePath,String[] columns,Workbook workbook,Sheet sheet){
        // 调整列宽以适应内容
        for (int i = 0; i < columns.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 写入到文件系统
        try {
            FileOutputStream outputStream = new FileOutputStream(filePath);
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close(); // 关闭工作簿释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void createExcelWithDataKuaiS(String filePath, List<Kuaishou> douyinList, String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表
        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (Kuaishou user : douyinList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getPinglun());
            row.createCell(2).setCellValue(user.getPinglunshijian());
            row.createCell(3).setCellValue(user.getName());
            row.createCell(4).setCellValue(user.getZhuye());
        }
        weibu(filePath,columns,workbook,sheet);
    }


    //String[] columns = {"关键字", "评论", "评论时间", "地址", "名称", "主页"};
    public static void createExcelWithDataDy(String filePath, List<Douyin> douyinList, String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表
        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (Douyin user : douyinList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getPinglun());
            row.createCell(2).setCellValue(user.getPinglunshijian());
            row.createCell(3).setCellValue(user.getAddress());
            row.createCell(4).setCellValue(user.getName());
            row.createCell(5).setCellValue(user.getZhuye());
        }
        weibu(filePath,columns,workbook,sheet);
    }

    public static void createExcelWithDataDyhuifupinglun(String filePath, List<Douyin> douyinList,String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表
        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (Douyin user : douyinList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getAddress());
            row.createCell(2).setCellValue(user.getName());
            row.createCell(3).setCellValue(user.getZhuye());
            row.createCell(4).setCellValue(user.getPinglun());
            row.createCell(5).setCellValue(user.getPinglunshijian());
            row.createCell(6).setCellValue(user.getHuifu());
        }
        weibu(filePath,columns,workbook,sheet);
    }

    public static void createExcelWithDataXHSPinglun2(String filePath, List<XiaoHS> xhsList, String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表

        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (XiaoHS user : xhsList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getPinglun());
            row.createCell(2).setCellValue(user.getShijian());
            row.createCell(3).setCellValue(user.getAddress());
            row.createCell(4).setCellValue(user.getName());
            row.createCell(5).setCellValue(user.getLink());
        }

        weibu(filePath,columns,workbook,sheet);
    }

    public static void createExcelWithDataXHSBiji1(String filePath, List<XiaoHS> xhsList,String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表

        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (XiaoHS user : xhsList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getZuozhe());
            row.createCell(2).setCellValue(user.getName());
            row.createCell(3).setCellValue(user.getLink());
            row.createCell(4).setCellValue(user.getDianzan());
            row.createCell(5).setCellValue(user.getShijian());
        }

        weibu(filePath,columns,workbook,sheet);
    }

    public static void createExcelWithDataXHSHuifuPinglun5(String filePath, List<XiaoHS> xhsList, String[] columns) {
        Workbook workbook = new XSSFWorkbook(); // 创建工作簿对象
        Sheet sheet = workbook.createSheet("sheet1"); // 创建工作表

        toubu(columns,workbook,sheet);
        // 填充数据行
        int rowNum = 1;
        for (XiaoHS user : xhsList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getGaunjianzi());
            row.createCell(1).setCellValue(user.getPinglun());
            row.createCell(2).setCellValue(user.getShijian());
            row.createCell(3).setCellValue(user.getAddress());
            row.createCell(4).setCellValue(user.getName());
            row.createCell(5).setCellValue(user.getLink());
            row.createCell(6).setCellValue(user.getHuifu());
        }

        weibu(filePath,columns,workbook,sheet);
    }
}
